﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LinqToTwitter;
using System.Configuration;
using System.Threading;

namespace Twitter.Util
{
    public class Authentification
    {

        public WebAuthorizer auth
        {
            set;
            get;
        }


        public Boolean isAuthentificated(HttpRequest Request, HttpResponse Response)
        {
            IOAuthCredentials credentials = new SessionStateCredentials();

            if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
            {
                //Récupere les ConsumerKey et ConsumerSecret
                credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
                credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
            }

            auth = new WebAuthorizer
            {
                Credentials = credentials,
                PerformRedirect = authUrl => Response.Redirect(authUrl)
            };


            Thread.Sleep(1000);
            auth.CompleteAuthorization(Request.Url);


            if (string.IsNullOrWhiteSpace(credentials.ConsumerKey) ||
                string.IsNullOrWhiteSpace(credentials.ConsumerSecret))
            {

                return false;
            }
            else if (auth.IsAuthorized)
            {
                return true;
            }
            else
            {
                return false;
            }
        
        }


    }
}